package com.pls.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.pls.actionAdapter.PlsAdapter;
import com.pls.javabeans.LoginBean;


/**
 * Servlet implementation class ValidateLogin
 */
@WebServlet("/ValidateLogin")
public class ValidateLogin extends HttpServlet {
	Logger logger = Logger.getLogger(ValidateLogin.class);
	private static final long serialVersionUID = 1L;


	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
		
//		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		process(request, response);

	}
	
	protected void process(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session = request.getSession();
		if(session.getAttribute("userRole") == null){
			
			PlsAdapter adapter = new PlsAdapter();
			LoginBean bean = new LoginBean();
			String googleEmail = (String) request.getParameter("googleEmail");
			if (!googleEmail.equals("")){	
				logger.info("Validating email " + googleEmail + " from database");
				bean = adapter.login(googleEmail);
				
				if(bean.getEmployeeWorksation() == null){
					RequestDispatcher rd = request.getRequestDispatcher("pages/login.jsp");				
					request.setAttribute("hasErrorMsg", "email is not registered in database");
					logger.info("Username and password not matched");
					rd.forward(request, response);
					
				}else{
					session.setAttribute("userRole", bean.getEmployeeWorksation().getEmployee().getUserRole());
					session.setAttribute("employeeWorkStation", bean);
					logger.info("Validation success... redirecting to home page.");
					response.sendRedirect("pages/home.jsp");
				}
				
			}else{
				String emailAddress = (String) request.getParameter("username");
				String password = (String) request.getParameter("password");
				bean = adapter.login(emailAddress, password);
				if(bean.getEmployeeWorksation() == null){
					RequestDispatcher rd = request.getRequestDispatcher("pages/login.jsp");				
					request.setAttribute("hasErrorMsg", "username and password doesn't match");
					logger.info("Username and password not matched");
					rd.forward(request, response);					
				}else{
					session.setAttribute("userRole", bean.getEmployeeWorksation().getEmployee().getUserRole());
					session.setAttribute("employeeWorkStation", bean);
					logger.info("Validation success... redirecting to home page.");
					response.sendRedirect("pages/home.jsp");
				}
				
			}
		}
			
		else{
			logger.debug("user log in as: " + session.getAttribute("userRole") + " redirecting to homepage" );
			response.sendRedirect("pages/home.jsp");
		}

		
		
		
		
		
		
		
		
		
		
		
		

		
		

	}

}
